//
//  ChooseTF.m
//  LClass
//
//  Created by 严军 on 16/5/30.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "ChooseTF.h"

@implementation ChooseTF

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init
{
    self=[super init];
    if (self) {
        self.frame=CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.alpha=0.0;
        [self initLayout];
    }
    return self;
}

-(void)initLayout
{
    //创建tableview
    
    UITableView* tableview  =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth*0.7, ScreenHeight*0.7) style:UITableViewStylePlain];
    tableview.backgroundColor=[UIColor whiteColor];
    [self addSubview:tableview];
    _tableView=tableview;
    tableview.center=self.center;
    [[Utility sharedInstance] setLayerView:tableview borderW:1 borderColor:ThemeGrayColor radius:8];
}

#pragma mark tableView-delegat-datasorce

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identifi=@"cell";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identifi];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifi];
    }
    cell.textLabel.text=[[_dataList objectAtIndex:indexPath.row] objectForKey:@"courseName"];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataList count];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dissmiss];
    _chooseBlock([_dataList objectAtIndex:indexPath.row]);
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* head=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width,44)];
    head.backgroundColor=ThemeBackgroundColor;
    UILabel* lab=[[UILabel alloc]init];
    [head addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(head);
    }];
    lab.text=@"请选择课程";
    lab.font=[UIFont boldSystemFontOfSize:18];
    lab.textAlignment=NSTextAlignmentCenter;
    lab.textColor=TitleColorLight;
    
    UIView* line=[[UIView alloc]init];
    [head addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(head);
        make.height.mas_equalTo(1);
        make.top.equalTo(head.mas_bottom).offset(-1);
    }];
    line.backgroundColor=TitleColorLight;
    return head;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

-(void)popView
{
    
    if ([[Utility sharedInstance]arryIsNotNull:_dataList]) {
        _tableView.frame=CGRectMake(0, 0, ScreenWidth*0.7, (_dataList.count+1)*44<ScreenHeight*0.7?(_dataList.count+1)*44:ScreenHeight*0.7);
        _tableView.center=self.center;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [[[UIApplication sharedApplication].delegate window] addSubview:self];
        [UIView beginAnimations:@"Pop" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationDelegate:self];
         self.alpha=1.0;
        [UIView commitAnimations];
    }
    
   
    
}

-(void)dissmiss
{
    [UIView beginAnimations:@"Pop" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    self.alpha=0.0;
    [UIView commitAnimations];
}

@end
